package com.yiqi.zhiyuan.feature.main.view;

import android.content.pm.ActivityInfo;
import android.os.Build;
import android.support.v4.app.Fragment;
import android.support.v4.content.ContextCompat;
import android.text.format.DateUtils;
import android.text.style.ForegroundColorSpan;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.FrameLayout;

import com.flyco.tablayout.CommonTabLayout;
import com.flyco.tablayout.listener.CustomTabEntity;
import com.gyf.immersionbar.ImmersionBar;
import com.suozhang.framework.component.websocket.WSHost;
import com.suozhang.framework.component.websocket.WsManager;
import com.suozhang.framework.component.websocket.listener.WsStatusListener;
import com.suozhang.framework.framework.AM;
import com.suozhang.framework.framework.BaseActivity;
import com.suozhang.framework.utils.T;
import com.yiqi.zhiyuan.R;
import com.yiqi.zhiyuan.entity.bo.TabEntity;
import com.yiqi.zhiyuan.feature.home.view.HomeFragment;
import com.yiqi.zhiyuan.feature.mine.view.MineFragment;
import com.yiqi.zhiyuan.feature.order.view.OrderFragment;

import java.util.ArrayList;
import java.util.concurrent.TimeUnit;

import butterknife.BindView;
import okhttp3.OkHttpClient;
import okhttp3.Response;
import okio.ByteString;

import static com.suozhang.framework.utils.AppUtil.fullScreen;


/**
 * @author lijuw
 */
public class MainActivity extends BaseActivity {


    @BindView(R.id.fl_change)
    FrameLayout mFlChange;

    @BindView(R.id.tab_home)
    CommonTabLayout mTabHome;

    private ArrayList<Fragment> mFragments = new ArrayList<>();


    private String[] mTitles = {"首页", "订单", "我的"};

    private int[] mIconUnselectIds = {
            R.mipmap.tab_home_unselect, R.mipmap.tab_order_unselect,
            R.mipmap.tab_mine_unselect};

    private int[] mIconSelectIds = {
            R.mipmap.tab_home_select, R.mipmap.tab_order_select,
            R.mipmap.tab_mine_select};
    private ArrayList<CustomTabEntity> mTabEntities = new ArrayList<>();

    private WsManager wsManager;

    @Override
    protected int attachLayoutRes() {
        return R.layout.activity_main;
    }

    @Override
    protected void initView() {
        initTab();
    }

    @Override
    protected void initInjector() {

    }

    @Override
    protected void initData() {

    }

    @Override
    protected void initSystemParams() {
        fullScreen(this);
    }

    private  void initTab(){
        mFragments.add(HomeFragment.getInstance(mTitles[0]));
        mFragments.add(OrderFragment.getInstance(mTitles[1]));
        mFragments.add(MineFragment.getInstance(mTitles[2]));

        for (int i = 0; i < mTitles.length; i++) {
            mTabEntities.add(new TabEntity(mTitles[i], mIconSelectIds[i], mIconUnselectIds[i]));
        }

        mTabHome.setTabData(mTabEntities, this, R.id.fl_change, mFragments);
    }

    private long exitTime = 0;

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {

        if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN) {

            if ((System.currentTimeMillis() - exitTime) > 2000) {
                T.showShort(R.string.toast_app_exit);
                exitTime = System.currentTimeMillis();
            } else {
                AM.appManager().exitApp();
            }

            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    private WsStatusListener wsStatusListener = new WsStatusListener() {

        public void onMessage(String text) {

        }

        @Override
        public void onReconnect() {

        }

        @Override
        public void onClosing(int code, String reason) {

        }

        @Override
        public void onClosed(int code, String reason){
        }

        @Override
        public void onFailure(Throwable t, Response response) {

        }
    };


    private void initStart(){
        if (wsManager != null) {
            wsManager.stopConnect();
            wsManager = null;
        }
        wsManager = new WsManager.Builder(getBaseContext()).client(
                new OkHttpClient().newBuilder()
                        .retryOnConnectionFailure(true)
                        .readTimeout(5, TimeUnit.SECONDS)//设置读取超时时间
                        .writeTimeout(5, TimeUnit.SECONDS)//设置写的超时时间
                        .connectTimeout(5, TimeUnit.SECONDS)//设置连接超时时间
                        .build())
                .needReconnect(true)
                .wsUrl(WSHost.WSURL)
                .build();
        wsManager.setWsStatusListener(wsStatusListener);
        wsManager.startConnect();
    }

}
